package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysMenuDao;
import com.gusha.dao.SysRoleMenuDao;
import com.gusha.model.SysMenu;
import com.gusha.model.SysRole;
import com.gusha.model.SysRoleMenu;
import com.gusha.utils.JsonResult;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysMenuService服务类
 */

@Service
@Transactional
public class SysMenuService {
	
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
	private SysRoleMenuDao sysRoleMenuDao;
    

    public Page<SysMenu> findAll(Pageable pageable, SysMenu sysMenu) {
        @SuppressWarnings("serial")
        Page<SysMenu> result = sysMenuDao.findAll(new Specification<SysMenu>() {
            @Override
            public Predicate toPredicate(Root<SysMenu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysMenu.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysMenu.getId()));
                }
                if (StringUtils.isNotBlank(sysMenu.getMenuName())) {
                    list.add(cb.like(root.get("menuName").as(String.class),"%"+sysMenu.getMenuName()+"%"));
                }
                if (StringUtils.isNotBlank(sysMenu.getMenuUrl())) {
                    list.add(cb.equal(root.get("menuUrl").as(String.class),sysMenu.getMenuUrl()));
                }
                if (StringUtils.isNotBlank(sysMenu.getMenuDesc())) {
                    list.add(cb.equal(root.get("menuDesc").as(String.class),sysMenu.getMenuDesc()));
                }
                if (StringUtils.isNotBlank(sysMenu.getMenuIcon())) {
                    list.add(cb.equal(root.get("menuIcon").as(String.class),sysMenu.getMenuIcon()));
                }
                if (sysMenu.getMenuLevel() != null) {
                    list.add(cb.equal(root.get("menuLevel").as(Integer.class),sysMenu.getMenuLevel()));
                }
                if (sysMenu.getParentId() != null) {
                    list.add(cb.equal(root.get("parentId").as(BigInteger.class),sysMenu.getParentId()));
                }
                if (sysMenu.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),sysMenu.getCreateTime()));
                }
                if (sysMenu.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysMenu.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(sysMenu.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),sysMenu.getCreator()));
                }
                if (sysMenu.getSort() != null) {
                    list.add(cb.equal(root.get("sort").as(Integer.class),sysMenu.getSort()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

	public List<SysMenu> getOneAndTwoMenuListByRole(HttpSession session) {
		SysRole role = (SysRole) session.getAttribute("role");
		List<SysMenu> list = sysRoleMenuDao.getMenuByRole(role);
		for (int i = 0; i < list.size(); i++) {
			List<SysMenu> list2 = sysRoleMenuDao.getSecondMenuByRoleAndMenu(role, list.get(i).getId());
			list.get(i).setList(list2);
		}
		return list;
	}

	public JsonResult deleteMenu(BigInteger id) {
		JsonResult result = new JsonResult(false);
		result.setMsg("删除失败");
		SysMenu menu = sysMenuDao.getOne(id);
		Integer layer = menu.getMenuLevel();
		List<SysMenu> listDel = new ArrayList<SysMenu>();
		List<SysRoleMenu> listRMDel = new ArrayList<SysRoleMenu>();
		if (layer == 1) {// 1级菜单删除下面所有的
			listDel.add(menu);
			List<SysMenu> list = sysMenuDao.findAllByParentIdOrderBySortDesc(menu.getId());
			listDel.addAll(list);
			for (int i = 0; i < list.size(); i++) {
				SysMenu menu2 = list.get(i);
				listDel.add(menu2);
				List<SysMenu> list2 = sysMenuDao.findAllByParentIdOrderBySortDesc(menu2.getId());
				listDel.addAll(list2);
			}
			// 删除菜单
			if (listDel.size() > 0) {
				sysMenuDao.deleteAll(listDel);
			}
			// 删除角色菜单
			for (int i = 0; i < listDel.size(); i++) {
				List<SysRoleMenu> list3 = sysRoleMenuDao.findAllByMenu_Id(listDel.get(i).getId());
				listRMDel.addAll(list3);
			}
			if (listRMDel.size() > 0) {
				sysRoleMenuDao.deleteAll(listRMDel);
			}
		}
		if (layer == 2) {// 2级菜单删除下面所有的
			List<SysMenu> list = sysMenuDao.findAllByParentIdOrderBySortDesc(menu.getId());
			listDel.add(menu);
			listDel.addAll(list);
			// 删除菜单
			if (listDel.size() > 0) {
				sysMenuDao.deleteAll(listDel);
			}
			// 删除角色菜单
			for (int i = 0; i < listDel.size(); i++) {
				List<SysRoleMenu> list3 = sysRoleMenuDao.findAllByMenu_Id(listDel.get(i).getId());
				listRMDel.addAll(list3);
			}
			if (listRMDel.size() > 0) {
				sysRoleMenuDao.deleteAll(listRMDel);
			}
		}
		result.setFlag(true);
		result.setMsg("删除成功");
		return result;
	}

	public List<SysMenu> getOneAndTwoMenuListByAll() {
		List<SysMenu> list = sysMenuDao.findAllByMenuLevelOrderBySortDesc(1);
		for (int i = 0; i < list.size(); i++) {
			List<SysMenu> list2 = sysMenuDao.findAllByMenuLevelAndParentIdOrderBySortDesc(2,list.get(i).getId());
			list.get(i).setList(list2);
		}
		return list;
	}
}
